package com.sion.testvol;

import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.provider.Settings;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity {

    private AudioManager audioManager;
    private NotificationManager notificationManager;
    private static final int REQUEST_NOTIFICATION_POLICY_ACCESS = 1001;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        // 初始化音频管理器
        audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        // 设置按钮点击事件
        Button btnMaxVolume = findViewById(R.id.btnMaxVolume);
        btnMaxVolume.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                maximizeAllVolumes();
            }
        });
    }

    /**
     * 将所有音量调至最大并取消静音和勿扰模式
     */
    private void maximizeAllVolumes() {
        // 检查勿扰模式权限
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (!notificationManager.isNotificationPolicyAccessGranted()) {
                // 请求勿扰模式权限
                Intent intent = new Intent(Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);
                startActivityForResult(intent, REQUEST_NOTIFICATION_POLICY_ACCESS);
                Toast.makeText(this, "请授予勿扰模式访问权限", Toast.LENGTH_LONG).show();
                return;
            }
        }

        // 取消勿扰模式
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            notificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_ALL);
        }

        // 取消静音模式
        audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);

        // 获取各种音量的最大值
        int maxRinger = audioManager.getStreamMaxVolume(AudioManager.STREAM_RING);
        int maxMusic = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        int maxAlarm = audioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM);
        int maxVoiceCall = audioManager.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL);
        int maxSystem = audioManager.getStreamMaxVolume(AudioManager.STREAM_SYSTEM);
        int maxNotification = audioManager.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION);

        // 将所有音量调至最大
        audioManager.setStreamVolume(AudioManager.STREAM_RING, maxRinger, AudioManager.FLAG_SHOW_UI);
        audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, maxMusic, AudioManager.FLAG_SHOW_UI);
        audioManager.setStreamVolume(AudioManager.STREAM_ALARM, maxAlarm, AudioManager.FLAG_SHOW_UI);
        audioManager.setStreamVolume(AudioManager.STREAM_VOICE_CALL, maxVoiceCall, AudioManager.FLAG_SHOW_UI);
        audioManager.setStreamVolume(AudioManager.STREAM_SYSTEM, maxSystem, AudioManager.FLAG_SHOW_UI);
        audioManager.setStreamVolume(AudioManager.STREAM_NOTIFICATION, maxNotification, AudioManager.FLAG_SHOW_UI);

        Toast.makeText(this, "已将所有音量调至最大并取消静音和勿扰模式，3秒后将关闭应用", Toast.LENGTH_SHORT).show();
        
        // 3秒后关闭应用
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                finish(); // 关闭当前Activity
            }
        }, 1500); // 1500毫秒
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_NOTIFICATION_POLICY_ACCESS) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (notificationManager.isNotificationPolicyAccessGranted()) {
                    // 用户已授予权限，再次尝试调整音量
                    maximizeAllVolumes(); // 这里会自动处理3秒后关闭应用
                } else {
                    Toast.makeText(this, "未授予勿扰模式访问权限，部分功能可能无法正常工作", Toast.LENGTH_LONG).show();
                }
            }
        }
    }
}